<!--
	地震烈度评定结果及调查数据
	Created by Penglei on 2023/08/08
-->
<template>
  <div class="app-container">
    <!-- 公共顶部 -->
    <large-screen-header />
    <!-- 主体部分 -->
    <div class="app-main">
      <!-- 左侧 -->
      <div class="app-left">
        <div class="app-left-item" style="height: 300px;">
          <subheadings name="各评估子区抽样点个数" />
          <SubareaSampling></SubareaSampling>
        </div>
        <div class="app-left-item" style="height: 300px;">
          <subheadings name="评估子区一破坏面积数据统计" />
          <DamagedArea></DamagedArea>
        </div>
        <div class="app-left-item" style="height: 300px;">
          <subheadings name="各类直接经济损失值（元）" />
          <PecuniaryLosses></PecuniaryLosses>
        </div>
      </div>
      <!-- 中心部分 -->
      <div class="app-center">
        <!-- 标题 -->
        <headline name="地震烈度评定结果及调查数据" />
        <!-- 地图 -->
        <div class="app-map">
          <SystemSelect :styles="{ position: 'absolute', top: '-30px', left: '42px' }" />
          <!-- <IndexMap></IndexMap> -->
          <OlMap :refId="'intensityassessment'" style="height: 100%" @getMap="getMap" @mapClick="mapClick"></OlMap>
          <div ref="popupElement" class="popup-bg" id="map-popup">
            <div class="triangle"></div>
            <div class="popup-head">
              <div class="popup-head-title">XXX县历年来地震灾损情况表</div>
              <div class="popup-head-x">X</div>
            </div>
            <div class="popup-content">
              <div class="popup-table">
                <el-table max-height="150" :data="tableData">
                  <el-table-column prop="time" label="发震时间" :header-align="headerAlign">
                    <template #default="scope">
                      <div class="centered-cell">{{ scope.row.time }}</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="level" label="震级" :header-align="headerAlign">
                    <template #default="scope">
                      <div style="color: #46ffff" class="centered-cell">{{ scope.row.level }}</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="lost" label="损失值" :header-align="headerAlign">
                    <template #default="scope">
                      <div style="color: #46ffff" class="centered-cell">{{ scope.row.lost }}</div>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 右侧 -->
      <div class="app-right">
        <div class="app-right-item" style="height: 480px;">
          <subheadings name="现场调查人员和车辆分布图" />
          <div style="margin: 10px; width: 430px; height: 380px;">
            <img src="./images/map.png" alt="" style="width: 100%;">
          </div>
        </div>
        <div class="app-right-item" style="height: 425px;">
          <subheadings name="现场各类型人员和车辆占比情况" />
          <ProportionPersonnelVehicles></ProportionPersonnelVehicles>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="IntensityAssessment">
// 各评估子区抽样点个数
import SubareaSampling from './components/SubareaSampling';
// 评估子区一破坏面积数据统计
import DamagedArea from './components/DamagedArea';
// 各类直接经济损失值（元）
import PecuniaryLosses from './components/PecuniaryLosses';
// 现场各类型人员和车辆占比情况
import ProportionPersonnelVehicles from './components/ProportionPersonnelVehicles';

// 系统选择
import SystemSelect from '@/components/SystemSelect';
import { getXcProportion, getVisitNum } from '@/api/lossAssessment.js';
// 现场数据占比
const XcProportion = ref({});
// 地震灾害损失评估访问统计
const SystemAchievementsStatistics = ref([]);
const isShow = ref(false);
const isShowData = ref(false);
// start
import OlMap from '@/components/Olmap';
import { Style, Circle as CircleStyle, Fill, Stroke, Icon, Text } from 'ol/style';
import Feature from 'ol/Feature';
import { Point } from 'ol/geom';
import Circle from 'ol/geom/Circle';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import { Vector as VectorSource } from 'ol/source';
import yellowEllipse from '@/assets/map/image/newImage/ellipse487.png';
import Overlay from 'ol/Overlay';
import { GeoJSON as GeoJSonFormat } from 'ol/format';
import { getChinaBaseMap } from '@/map/projMap.js';

getData();
function getData() {
  getXcProportion({ iaId: 2 }).then((res) => {
    XcProportion.value = res.data;
    isShowData.value = true;
  });

  // // 地震灾害损失评估访问统计
  getVisitNum({ system: 0, num: 5 }).then((res) => {
    SystemAchievementsStatistics.value = res.data;
    isShow.value = true;
  });
}

// end
// start
const tableData = [];
for (let i = 0; i < 10; i++) {
  tableData.push({
    level: i + 1,
    lost: +i,
    time: '2021-08-08',
  });
}
const mapInstance = ref(null);
const vectorLayer = ref(null);
const mapPopupLayer = ref(null);
const ellipseLayer = ref(null);
const popupElement = ref();

const getMap = (map) => {
  mapInstance.value = map.value;
  console.log('jjj', mapInstance.value);

  addProvinceNameLayer();

  //   假数据
  createVectorLayer();
  // createEllipseLayer();
  createPopUpLayer();
  addRandomPoints();
  // addEllipse();
};

function addProvinceNameLayer() {
  const geojsonSource = new VectorSource({
    url: getChinaBaseMap,
    format: new GeoJSonFormat(),
  });

  // 创建省份名称文本图层
  const textLayer = new VectorLayer({
    source: geojsonSource,
    zIndex: 3,
    style: function (feature) {
      const textstyle = new Style({
        text: new Text({
          font: '10px Calibri,sans-serif',
          fill: new Fill({ color: '#fff' }),
          text: feature.get('name'),
        }),
      });
      return textstyle;
    },
  });

  mapInstance.value.addLayer(textLayer);
}

const mapClick = (event) => {
  const features = mapInstance.value.getFeaturesAtPixel(event.pixel);
  if (features.length > 0) {
    const selectedFeature = features[0];
    // 获取要素的坐标信息
    popupElement.value.style.display = 'block';
    /**
    const coordinate = selectedFeature.getGeometry().getCoordinates();
    mapPopupLayer.value.setPosition(coordinate);
    // 获取地图容器的尺寸和点击事件的位置
    const mapContainerRect = mapInstance.value.getTarget().getBoundingClientRect();
    const clickX = event.pixel[0];
    const clickY = event.pixel[1];

    // 获取弹出框的尺寸
    const popupWidth = popupElement.value.offsetWidth;
    const popupHeight = popupElement.value.offsetHeight;

    // 默认的 positioning
    let positioning = 'top-left';

    // 判断弹出框是否超出地图容器的各个边界
    if (clickX + popupWidth > mapContainerRect.width) {
      positioning = 'top-right';
    } else if (clickX - popupWidth < 0) {
      positioning = 'top-left';
    }

    if (clickY + popupHeight > mapContainerRect.height) {
      positioning = positioning.replace('top', 'bottom');
    } else if (clickY - popupHeight < 0) {
      positioning = positioning.replace('bottom', 'top');
    }

    // 设置弹出框的位置和定位方式
    mapPopupLayer.value.setPositioning(positioning);
     */
    event.originalEvent.clientX += 20;
    event.originalEvent.clientY -= 5;
    let coordinate = mapInstance.value.getEventCoordinate(event.originalEvent);
    // 设置弹出框的位置
    mapPopupLayer.value.setPosition(coordinate);
    // 在这里添加处理点击事件的逻辑
    console.log('点击了要素：', selectedFeature.getProperties());
  } else {
    mapPopupLayer.value.setPosition(undefined);
  }
};
const createVectorLayer = () => {
  vectorLayer.value = new VectorLayer({
    source: new VectorSource(),
    zIndex: 2,
  });
  mapInstance.value.addLayer(vectorLayer.value);
};
const createEllipseLayer = () => {
  // 创建矢量图层
  ellipseLayer.value = new VectorLayer({
    source: new VectorSource(),
    zIndex: 1,
  });
  mapInstance.value.addLayer(ellipseLayer.value);
};
const addRandomPoints = () => {
  if (!vectorLayer.value) {
    return;
  }
  for (let i = 0; i < 1; i++) {
    const point = new Feature({
      geometry: new Point([116.403218, 39.92372]),
    });

    const style = new Style({
      image: new Icon({
        src: yellowEllipse,
        scale: 1,
      }),
    });

    point.setStyle(style);
    vectorLayer.value.getSource().addFeature(point);
  }
};
const createPopUpLayer = () => {
  let popup = document.getElementById('map-popup');
  mapPopupLayer.value = new Overlay({
    element: popup,
    positioning: 'center-left',
  });
  mapInstance.value.addOverlay(mapPopupLayer.value);
};
// 半径计算
function getRadius(radius) {
  let metersPerUnit = mapInstance.value.getView().getProjection().getMetersPerUnit();
  let circleRadius = radius / metersPerUnit;
  console.log('circleRadius', metersPerUnit, circleRadius);
  return circleRadius;
}
const addEllipse = () => {
  if (!mapInstance.value) {
    return;
  }
  // 创建大圆和小圆的要素
  const bigEllipseFeature = new Feature({
    geometry: new Circle([116.41257667603315, 40.185597596456724], getRadius(400000)),
  });
  const smallEllipseFeature = new Feature({
    geometry: new Circle(
      [116.41257667603315, 40.185597596456724],
      getRadius(200000) // 小圆的半径
    ),
  });

  // 设置大圆和小圆要素的样式
  bigEllipseFeature.setStyle(
    new Style({
      fill: new Fill({
        color: 'rgba(255, 37, 37, 0.3)', // 浅红色的半透明填充色
      }),
      stroke: new Stroke({
        color: 'red', // 红色边界线
        width: 1,
      }),
    })
  );

  smallEllipseFeature.setStyle(
    new Style({
      fill: new Fill({
        color: 'rgba(255, 37, 37, 0.30)', // 浅蓝色的半透明填充色
      }),
      stroke: new Stroke({
        color: 'blue', // 蓝色边界线
        width: 1,
      }),
    })
  );
  ellipseLayer.value.getSource().addFeature(smallEllipseFeature);
  ellipseLayer.value.getSource().addFeature(bigEllipseFeature);
};
// end
</script>

<style scoped lang="scss">
.app-container {
  min-height: 1080px;
}

.app-map {
  width: 960px;
  height: 100%;
  background-size: 100% 100%;
  overflow: visible;
}

/* start */
.popup-bg {
  min-width: 400px;
  height: 275px;
  /* background: linear-gradient(NaNdeg, rgba(5, 12, 53, 0.8) 0%, rgba(4, 16, 89, 0.8) 55%, rgba(6, 40, 150, 0.8) 100%); */
  background: rgba(6, 40, 150, 0.8);
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 2px solid #0190f9;
  display: flex;
  padding: 17px 15px;
  flex-direction: column;
  position: relative;
  .triangle {
    position: absolute;
    top: 50%;
    left: -10px;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-right: 10px solid #0190f9;
  }
  .popup-head {
    justify-content: center;

    display: flex;
    flex-direction: row;
    font-size: 18px;
    font-family: Source Han Sans CN-Bold, Source Han Sans CN;
    font-weight: bold;
    color: #ffffff;
    line-height: 23px;
    .popup-head-title {
      margin-left: auto;
    }
    .popup-head-x {
      margin-left: auto;
      width: 12px;
      height: 12px;
      color: #e6f4fd;
      opacity: 1;
    }
  }
  .popup-content {
    height: 100%;
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    font-size: 16px;
    font-family: Source Han Sans CN-Medium, Source Han Sans CN;
    font-weight: 500;
    color: #e6f4fd;
    line-height: 23px;
    .popup-content-title {
      background: #050426;
      border-radius: 0px 0px 0px 0px;
      opacity: 0.5;
      padding: 10px 34px;
      justify-content: space-between;
      display: flex;
      flex-direction: row;
    }
    .popup-table {
      width: 100%;
      height: 100%;
      margin-right: 20px;
      .el-table {
        width: 90%;
        max-height: 150px;
        overflow-x: hidden;
        overflow-y: auto;
        z-index: 99;
        position: absolute;
        background: rgba(0, 0, 0, 0);
        :deep(tr:hover > td) {
          background: rgba(71, 90, 219, 0.05) !important;
        }
        :deep(.el-table__header) {
          background: rgba(5, 4, 38, 0.4);
          tr {
            background-color: transparent !important;
          }
          th.el-table__cell {
            background-color: transparent !important;
          }
          th.el-table__cell.is-leaf {
            border-bottom: none;
          }
        }

        :deep(.el-table__row) {
          background: rgba(5, 4, 38, 0.4);
        }
        :deep(.el-table__inner-wrapper)::before {
          display: none;
        }
      }
    }
  }
}
/* .popup-bg::before {
  content: '';
  width: 0;
  height: 0;
  border: 15px solid;
  position: absolute;
  top: 50%;
  left: -43px;
  margin-top: -15px;
  border-color: transparent #0190f9 transparent transparent;
}
.popup-bg:after {
  content: '';
  width: 0;
  height: 0;
  border: 15px solid;
  position: absolute;
  top: 50%;
  left: -40px;
  margin-top: -15px;
  border-color: transparent rgba(6, 40, 150, 0.8) transparent transparent;
} */
:deep(.ol-overlaycontainer-stopevent) {
  z-index: 2;
}
/* end */
</style>
